package com.exmaple.lemlin.ui.activity;

import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.Toast;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.entity.AccessEvenBus;
import com.exmaple.lemlin.mvp.presenter.AccessPresenter;
import com.exmaple.lemlin.mvp.view.IAccessView;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseActivity;
import com.exmaple.lemlin.ui.fragment.AccessSignInFragment;
import com.exmaple.lemlin.ui.fragment.AccessSignUpFragment;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.utils.ToastUtil;
import com.exmaple.lemlin.xmpp.XmppConnection;

public class AccessActivity extends AbstractBaseActivity<AccessPresenter> implements IAccessView {

    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_access;
    }

    @Override
    protected void initView() {

        XmppConnection.getInstance().openConnection();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new AccessPresenter((this), (this));
        if (mPresenter.isAuthenticated()) {
            onSuccessLogin();
        } else {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.replace(R.id.frame_layout, AccessSignInFragment.newInstance());
            ft.commit();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.cancel();
        }
    }

    @Override
    public void onSuccessLogin() {
        Intent intent = new Intent((this), MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onSuccessRegister() {
        ToastUtil.showShort(APPIM.getContext(), "注册成功！");
        progressDialog.cancel();
        onBackPressed();
    }

    @Override
    public void onHint(String hint) {
        super.onHint(hint);
        progressDialog.cancel();
    }

    @Override
    public void onEventMainThread(AccessEvenBus evenBus) {
        super.onEventMainThread(evenBus);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCanceledOnTouchOutside(false);
        if (evenBus.getFragment() instanceof AccessSignInFragment) {
            progressDialog.setMessage("正在登录...");
            mPresenter.userLogin(evenBus.getAuthenticate());
        } else if (evenBus.getFragment() instanceof AccessSignUpFragment) {
            progressDialog.setMessage("正在注册...");
            mPresenter.userRegister(evenBus.getAuthenticate());
        }
        progressDialog.show();
    }

    public boolean isNotificationListenersEnabled() {
        String pkgName = getPackageName();
        final String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (String name : names) {
                final ComponentName cn = ComponentName.unflattenFromString(name);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    protected boolean gotoNotificationAccessSetting() {
        try {
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;

        } catch (ActivityNotFoundException e) {//普通情况下找不到的时候需要再特殊处理找一次
            try {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity");
                intent.setComponent(cn);
                intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
                startActivity(intent);
                return true;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            Toast.makeText(this, "对不起，您的手机暂不支持", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            return false;
        }
    }
}
